/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.ibrae.mchs.radiation.entities.controllers;

import ru.ibrae.mchs.radiation.entities.GSPos;
import ru.ibrae.mchs.radiation.entities.controllers.exceptions.NonexistentEntityException;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ru.ibrae.mchs.radiation.entities.SubjectRf;
import ru.ibrae.mchs.radiation.entities.Rajon;
import ru.ibrae.mchs.radiation.entities.Result;
import java.util.HashSet;
import java.util.Set;
import ru.ibrae.mchs.radiation.entities.Geoobject;

/**
 *
 * @author murr
 */
public class GSPosJpaController {

    public GSPosJpaController() {
        emf = Persistence.createEntityManagerFactory("DesktopApplication4PU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(GSPos GSPos) {
        if (GSPos.getResultSet() == null) {
            GSPos.setResultSet(new HashSet<Result>());
        }
        if (GSPos.getGeoobjectSet() == null) {
            GSPos.setGeoobjectSet(new HashSet<Geoobject>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            SubjectRf subjectRf = GSPos.getSubjectRf();
            if (subjectRf != null) {
                subjectRf = em.getReference(subjectRf.getClass(), subjectRf.getId());
                GSPos.setSubjectRf(subjectRf);
            }
            Rajon rajon = GSPos.getRajon();
            if (rajon != null) {
                rajon = em.getReference(rajon.getClass(), rajon.getIdRajon());
                GSPos.setRajon(rajon);
            }
            Set<Result> attachedResultSet = new HashSet<Result>();
            for (Result resultSetResultToAttach : GSPos.getResultSet()) {
                resultSetResultToAttach = em.getReference(resultSetResultToAttach.getClass(), resultSetResultToAttach.getIdRezult());
                attachedResultSet.add(resultSetResultToAttach);
            }
            GSPos.setResultSet(attachedResultSet);
            Set<Geoobject> attachedGeoobjectSet = new HashSet<Geoobject>();
            for (Geoobject geoobjectSetGeoobjectToAttach : GSPos.getGeoobjectSet()) {
                geoobjectSetGeoobjectToAttach = em.getReference(geoobjectSetGeoobjectToAttach.getClass(), geoobjectSetGeoobjectToAttach.getIdNp());
                attachedGeoobjectSet.add(geoobjectSetGeoobjectToAttach);
            }
            GSPos.setGeoobjectSet(attachedGeoobjectSet);
            em.persist(GSPos);
            if (subjectRf != null) {
                subjectRf.getGSPosSet().add(GSPos);
                subjectRf = em.merge(subjectRf);
            }
            if (rajon != null) {
                rajon.getGSPosSet().add(GSPos);
                rajon = em.merge(rajon);
            }
            for (Result resultSetResult : GSPos.getResultSet()) {
                GSPos oldGSPosOfResultSetResult = resultSetResult.getGSPos();
                resultSetResult.setGSPos(GSPos);
                resultSetResult = em.merge(resultSetResult);
                if (oldGSPosOfResultSetResult != null) {
                    oldGSPosOfResultSetResult.getResultSet().remove(resultSetResult);
                    oldGSPosOfResultSetResult = em.merge(oldGSPosOfResultSetResult);
                }
            }
            for (Geoobject geoobjectSetGeoobject : GSPos.getGeoobjectSet()) {
                GSPos oldGSPosOfGeoobjectSetGeoobject = geoobjectSetGeoobject.getGSPos();
                geoobjectSetGeoobject.setGSPos(GSPos);
                geoobjectSetGeoobject = em.merge(geoobjectSetGeoobject);
                if (oldGSPosOfGeoobjectSetGeoobject != null) {
                    oldGSPosOfGeoobjectSetGeoobject.getGeoobjectSet().remove(geoobjectSetGeoobject);
                    oldGSPosOfGeoobjectSetGeoobject = em.merge(oldGSPosOfGeoobjectSetGeoobject);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(GSPos GSPos) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            GSPos persistentGSPos = em.find(GSPos.class, GSPos.getIdPoselen());
            SubjectRf subjectRfOld = persistentGSPos.getSubjectRf();
            SubjectRf subjectRfNew = GSPos.getSubjectRf();
            Rajon rajonOld = persistentGSPos.getRajon();
            Rajon rajonNew = GSPos.getRajon();
            Set<Result> resultSetOld = persistentGSPos.getResultSet();
            Set<Result> resultSetNew = GSPos.getResultSet();
            Set<Geoobject> geoobjectSetOld = persistentGSPos.getGeoobjectSet();
            Set<Geoobject> geoobjectSetNew = GSPos.getGeoobjectSet();
            if (subjectRfNew != null) {
                subjectRfNew = em.getReference(subjectRfNew.getClass(), subjectRfNew.getId());
                GSPos.setSubjectRf(subjectRfNew);
            }
            if (rajonNew != null) {
                rajonNew = em.getReference(rajonNew.getClass(), rajonNew.getIdRajon());
                GSPos.setRajon(rajonNew);
            }
            Set<Result> attachedResultSetNew = new HashSet<Result>();
            for (Result resultSetNewResultToAttach : resultSetNew) {
                resultSetNewResultToAttach = em.getReference(resultSetNewResultToAttach.getClass(), resultSetNewResultToAttach.getIdRezult());
                attachedResultSetNew.add(resultSetNewResultToAttach);
            }
            resultSetNew = attachedResultSetNew;
            GSPos.setResultSet(resultSetNew);
            Set<Geoobject> attachedGeoobjectSetNew = new HashSet<Geoobject>();
            for (Geoobject geoobjectSetNewGeoobjectToAttach : geoobjectSetNew) {
                geoobjectSetNewGeoobjectToAttach = em.getReference(geoobjectSetNewGeoobjectToAttach.getClass(), geoobjectSetNewGeoobjectToAttach.getIdNp());
                attachedGeoobjectSetNew.add(geoobjectSetNewGeoobjectToAttach);
            }
            geoobjectSetNew = attachedGeoobjectSetNew;
            GSPos.setGeoobjectSet(geoobjectSetNew);
            GSPos = em.merge(GSPos);
            if (subjectRfOld != null && !subjectRfOld.equals(subjectRfNew)) {
                subjectRfOld.getGSPosSet().remove(GSPos);
                subjectRfOld = em.merge(subjectRfOld);
            }
            if (subjectRfNew != null && !subjectRfNew.equals(subjectRfOld)) {
                subjectRfNew.getGSPosSet().add(GSPos);
                subjectRfNew = em.merge(subjectRfNew);
            }
            if (rajonOld != null && !rajonOld.equals(rajonNew)) {
                rajonOld.getGSPosSet().remove(GSPos);
                rajonOld = em.merge(rajonOld);
            }
            if (rajonNew != null && !rajonNew.equals(rajonOld)) {
                rajonNew.getGSPosSet().add(GSPos);
                rajonNew = em.merge(rajonNew);
            }
            for (Result resultSetOldResult : resultSetOld) {
                if (!resultSetNew.contains(resultSetOldResult)) {
                    resultSetOldResult.setGSPos(null);
                    resultSetOldResult = em.merge(resultSetOldResult);
                }
            }
            for (Result resultSetNewResult : resultSetNew) {
                if (!resultSetOld.contains(resultSetNewResult)) {
                    GSPos oldGSPosOfResultSetNewResult = resultSetNewResult.getGSPos();
                    resultSetNewResult.setGSPos(GSPos);
                    resultSetNewResult = em.merge(resultSetNewResult);
                    if (oldGSPosOfResultSetNewResult != null && !oldGSPosOfResultSetNewResult.equals(GSPos)) {
                        oldGSPosOfResultSetNewResult.getResultSet().remove(resultSetNewResult);
                        oldGSPosOfResultSetNewResult = em.merge(oldGSPosOfResultSetNewResult);
                    }
                }
            }
            for (Geoobject geoobjectSetOldGeoobject : geoobjectSetOld) {
                if (!geoobjectSetNew.contains(geoobjectSetOldGeoobject)) {
                    geoobjectSetOldGeoobject.setGSPos(null);
                    geoobjectSetOldGeoobject = em.merge(geoobjectSetOldGeoobject);
                }
            }
            for (Geoobject geoobjectSetNewGeoobject : geoobjectSetNew) {
                if (!geoobjectSetOld.contains(geoobjectSetNewGeoobject)) {
                    GSPos oldGSPosOfGeoobjectSetNewGeoobject = geoobjectSetNewGeoobject.getGSPos();
                    geoobjectSetNewGeoobject.setGSPos(GSPos);
                    geoobjectSetNewGeoobject = em.merge(geoobjectSetNewGeoobject);
                    if (oldGSPosOfGeoobjectSetNewGeoobject != null && !oldGSPosOfGeoobjectSetNewGeoobject.equals(GSPos)) {
                        oldGSPosOfGeoobjectSetNewGeoobject.getGeoobjectSet().remove(geoobjectSetNewGeoobject);
                        oldGSPosOfGeoobjectSetNewGeoobject = em.merge(oldGSPosOfGeoobjectSetNewGeoobject);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = GSPos.getIdPoselen();
                if (findGSPos(id) == null) {
                    throw new NonexistentEntityException("The gSPos with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            GSPos GSPos;
            try {
                GSPos = em.getReference(GSPos.class, id);
                GSPos.getIdPoselen();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The GSPos with id " + id + " no longer exists.", enfe);
            }
            SubjectRf subjectRf = GSPos.getSubjectRf();
            if (subjectRf != null) {
                subjectRf.getGSPosSet().remove(GSPos);
                subjectRf = em.merge(subjectRf);
            }
            Rajon rajon = GSPos.getRajon();
            if (rajon != null) {
                rajon.getGSPosSet().remove(GSPos);
                rajon = em.merge(rajon);
            }
            Set<Result> resultSet = GSPos.getResultSet();
            for (Result resultSetResult : resultSet) {
                resultSetResult.setGSPos(null);
                resultSetResult = em.merge(resultSetResult);
            }
            Set<Geoobject> geoobjectSet = GSPos.getGeoobjectSet();
            for (Geoobject geoobjectSetGeoobject : geoobjectSet) {
                geoobjectSetGeoobject.setGSPos(null);
                geoobjectSetGeoobject = em.merge(geoobjectSetGeoobject);
            }
            em.remove(GSPos);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<GSPos> findGSPosEntities() {
        return findGSPosEntities(true, -1, -1);
    }

    public List<GSPos> findGSPosEntities(int maxResults, int firstResult) {
        return findGSPosEntities(false, maxResults, firstResult);
    }

    private List<GSPos> findGSPosEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(GSPos.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public GSPos findGSPos(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(GSPos.class, id);
        } finally {
            em.close();
        }
    }

    public int getGSPosCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<GSPos> rt = cq.from(GSPos.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
